﻿


create proc accounting.UpdateAccount2Company
@AccountID		int,
@OrgKey			int
as
begin
	set nocount on

	declare @rc int,
			@logText nvarchar(150)

	

	begin tran

	begin try
		if @OrgKey > 0
		begin
			delete	accounting.mnAccount2Company
			where	AccountID = @AccountID
					and OrgKey = @OrgKey
		end 

		if @OrgKey = -1 --Ef
		begin
			update accounting.AccountsStructure
			set		Ef = 0
			where AccountID = @AccountID
		end

		if @OrgKey = -2 --Og
		begin
			update accounting.AccountsStructure
			set		Og = 0
			where AccountID = @AccountID
		end

	end try
	begin catch
		select @AccountID 'AccountID', 0 'Return', ERROR_MESSAGE() 'Error'
		goto WithError
	end catch

	
	commit tran
	
	return 0

WithError:
	if @@TRANCOUNT > 0
	begin
		set @logText = 'AccountID: ' + cast(@AccountID as char(10)) + ' OrgKey: ' + cast(@OrgKey as char(10))
		exec @rc = dbo.SaveLog
			@UserID = 999, --Agent
			@LogType = 'S',
			@LogArea = 'Ac',
			@LogDetails = @logText,
			@IsError = 1
		rollback tran
	end
	return -1
end